home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / term / term_47a_pch.lha / Source / term-4.7a / Print.c < prev    next >
C/C++ Source or Header  |  1996-11-02  |  24KB  |  1,149 lines

  1. /*
  2. **    Print.c
  3. **
  4. **    Printer control routines
  5. **
  6. **    Copyright © 1990-1996 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. **
  9. **    :ts=4
  10. */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "Global.h"
  14. #endif
  15.  
  16.     /* PrintText(BPTR File,struct Window *ReqWindow,LONG *Error,STRPTR String,...):
  17.      *
  18.      *    Output a printf() style message.
  19.      */
  20.  
  21. BOOL
  22. PrintText(BPTR File,struct Window *ReqWindow,LONG *Error,STRPTR String,...)
  23. {
  24.     UBYTE LocalBuffer[256];
  25.     va_list    VarArgs;
  26.     LONG Len;
  27.  
  28.     va_start(VarArgs,String);
  29.     LimitedVSPrintf(sizeof(LocalBuffer),LocalBuffer,String,VarArgs);
  30.     va_end(VarArgs);
  31.  
  32.     if(ReqWindow)
  33.     {
  34.         if(!SysReqHandler(ReqWindow,NULL,FALSE))
  35.         {
  36.             *Error = 0;
  37.  
  38.             return(FALSE);
  39.         }
  40.     }
  41.     else
  42.     {
  43.         if(SetSignal(0,SIG_BREAK) & SIG_BREAK)
  44.         {
  45.             *Error = 0;
  46.  
  47.             return(FALSE);
  48.         }
  49.     }
  50.  
  51.     Len = strlen(LocalBuffer) + 1;
  52.  
  53.     SetIoErr(0);
  54.  
  55.     LimitedStrcat(sizeof(LocalBuffer),LocalBuffer,"\n");
  56.  
  57.     if(Write(File,LocalBuffer,Len) < Len)
  58.     {
  59.         *Error = IoErr();
  60.  
  61.         return(FALSE);
  62.     }
  63.     else
  64.         return(TRUE);
  65. }
  66.  
  67.     /* PrintHeader(BPTR File,struct Window *ReqWindow,LONG *Error,ULONG Code):
  68.      *
  69.      *    Print a line header.
  70.      */
  71.  
  72. STATIC BOOL
  73. PrintHeader(BPTR File,struct Window *ReqWindow,LONG *Error,ULONG Code,BOOL Plain)
  74. {
  75.     UBYTE LocalBuffer[256];
  76.     STRPTR String;
  77.     LONG Len;
  78.  
  79.     String = LocaleString(Code);
  80.  
  81.     if(ReqWindow)
  82.     {
  83.         if(!SysReqHandler(ReqWindow,NULL,FALSE))
  84.         {
  85.             *Error = 0;
  86.  
  87.             return(FALSE);
  88.         }
  89.     }
  90.     else
  91.     {
  92.         if(SetSignal(0,SIG_BREAK) & SIG_BREAK)
  93.         {
  94.             *Error = 0;
  95.  
  96.             return(FALSE);
  97.         }
  98.     }
  99.  
  100.     if(!Plain)
  101.     {
  102.         LimitedSPrintf(sizeof(LocalBuffer),LocalBuffer,"\33[1m%s\33[0m",String);
  103.         String = LocalBuffer;
  104.     }
  105.  
  106.     SetIoErr(0);
  107.  
  108.     Len = strlen(String);
  109.  
  110.     if(Write(File,String,Len) < Len)
  111.     {
  112.         *Error = IoErr();
  113.  
  114.         return(FALSE);
  115.     }
  116.  
  117.     return(TRUE);
  118. }
  119.  
  120.     /* PrintFileInformation():
  121.      *
  122.      *    Print information on a file.
  123.      */
  124.  
  125. BOOL
  126. PrintFileInformation(BPTR File,struct Window *ReqWindow,LONG *Error,STRPTR Name,ULONG Flags)
  127. {
  128.     BOOL Continue;
  129.  
  130.         /* Any special information to print along with the name? */
  131.  
  132.     if(Flags)
  133.     {
  134.         BPTR FileLock;
  135.  
  136.             /* Try to grip the file. */
  137.  
  138.         if(FileLock = Lock(Name,ACCESS_READ))
  139.         {
  140.             D_S(struct FileInfoBlock,FileInfo);
  141.  
  142.                 /* How does it look like? */
  143.  
  144.             if(Examine(FileLock,FileInfo))
  145.             {
  146.                 UBYTE DummyBuffer[300];
  147.                 STRPTR Index;
  148.  
  149.                     /* Add the size. */
  150.  
  151.                 if(Flags & PRINT_SIZE)
  152.                     LimitedSPrintf(sizeof(DummyBuffer),DummyBuffer,"%-25s %7ld",FilePart(Name),FileInfo->fib_Size);
  153.                 else
  154.                     LimitedSPrintf(sizeof(DummyBuffer),DummyBuffer,"%-25s",FilePart(Name));
  155.  
  156.                 Index = DummyBuffer;
  157.  
  158.                     /* Find the end of the string. */
  159.  
  160.                 while(*Index)
  161.                     Index++;
  162.  
  163.                     /* Add the protection bits. */
  164.  
  165.                 if(Flags & PRINT_BITS)
  166.                 {
  167.                     STATIC STRPTR    SetBits = "----aps",
  168.                                     ClrBits = "dewr---";
  169.  
  170.                     UBYTE TempString[10];
  171.  
  172.                     LONG i;
  173.  
  174.                     strcpy(TempString," -------");
  175.  
  176.                     for(i = 0 ; i < 7 ; i++)
  177.                     {
  178.                         if(FileInfo->fib_Protection & (1L << i))
  179.                             TempString[6 - i + 1] = SetBits[i];
  180.                         else
  181.                             TempString[6 - i + 1] = ClrBits[i];
  182.                     }
  183.  
  184.                     strcpy(Index,TempString);
  185.  
  186.                     while(*Index)
  187.                         Index++;
  188.                 }
  189.  
  190.                     /* Add the creation date. */
  191.  
  192.                 if(Flags & PRINT_DATE)
  193.                 {
  194.                     UBYTE Date[20],Time[20];
  195.                     struct DateTime    DateTime;
  196.  
  197.                         /* Prepare for date conversion. */
  198.  
  199.                     DateTime.dat_Stamp        = FileInfo->fib_Date;
  200.                     DateTime.dat_Format        = FORMAT_DEF;
  201.                     DateTime.dat_Flags        = DTF_SUBST;
  202.                     DateTime.dat_StrDay        = NULL;
  203.                     DateTime.dat_StrDate    = Date;
  204.                     DateTime.dat_StrTime    = Time;
  205.  
  206.                         /* Convert the date. */
  207.  
  208.                     if(DateToStr(&DateTime))
  209.                     {
  210.                         LimitedSPrintf(sizeof(DummyBuffer) - ((LONG)Index - (LONG)DummyBuffer),Index," %-9s %s",Date,Time);
  211.  
  212.                         while(*Index)
  213.                             Index++;
  214.                     }
  215.                 }
  216.  
  217.                     /* Add the file comment. */
  218.  
  219.                 if(Flags & PRINT_COMMENT)
  220.                     LimitedSPrintf(sizeof(DummyBuffer) - ((LONG)Index - (LONG)DummyBuffer),Index,"\n: %s",FileInfo->fib_Comment);
  221.  
  222.                 Continue = PrintText(File,ReqWindow,Error,"%s\n",DummyBuffer);
  223.             }
  224.             else
  225.                 Continue = FALSE;
  226.  
  227.             UnLock(FileLock);
  228.         }
  229.         else
  230.             Continue = FALSE;
  231.     }
  232.     else
  233.         Continue = PrintText(File,ReqWindow,Error,"%s\n",Name);
  234.  
  235.     return(Continue);
  236. }
  237.  
  238.     /* PrintEntry(BPTR File,struct Window *ReqWindow,LONG *Error,struct PhoneEntry *Entry):
  239.      *
  240.      *    Print information on the contents of a phonebook entry.
  241.      */
  242.  
  243. BOOL
  244. PrintEntry(BPTR File,struct Window *ReqWindow,BOOL Plain,LONG *Error,struct PhoneEntry *Entry,ULONG Flags)
  245. {
  246.     if(Plain)
  247.     {
  248.         if(!PrintText(File,ReqWindow,Error,"\n\"%s\" (%s)",Entry->Header->Name,Entry->Header->Number))
  249.             return(FALSE);
  250.     }
  251.     else
  252.     {
  253.         if(!PrintText(File,ReqWindow,Error,"\n\33[4m\"%s\" (%s)\33[0m",Entry->Header->Name,Entry->Header->Number))
  254.             return(FALSE);
  255.     }
  256.  
  257.     if(Flags & PRINT_COMMENT)
  258.     {
  259.         if(Entry->Header->Comment[0])
  260.         {
  261.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_COMMENT_TXT,Plain))
  262.                 return(FALSE);
  263.  
  264.             if(!PrintText(File,ReqWindow,Error,Entry->Header->Comment))
  265.                 return(FALSE);
  266.         }
  267.     }
  268.  
  269.     if(Flags & PRINT_USERNAME)
  270.     {
  271.         if(Entry->Header->UserName[0])
  272.         {
  273.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_USER_NAME_TXT,Plain))
  274.                 return(FALSE);
  275.  
  276.             if(!PrintText(File,ReqWindow,Error,Entry->Header->UserName))
  277.                 return(FALSE);
  278.         }
  279.     }
  280.  
  281.     if((Flags & PRINT_SERIAL) && Entry->Config->SerialConfig)
  282.     {
  283.         STATIC UBYTE Parities[] =
  284.         {
  285.             'N','E','O','M','S'
  286.         };
  287.  
  288.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_BAUD_RATE_TXT,Plain))
  289.             return(FALSE);
  290.  
  291.         if(LocaleBase)
  292.         {
  293.             if(!PrintText(File,ReqWindow,Error,"%lD",Entry->Config->SerialConfig->BaudRate))
  294.                 return(FALSE);
  295.         }
  296.         else
  297.         {
  298.             if(!PrintText(File,ReqWindow,Error,"%ld",Entry->Config->SerialConfig->BaudRate))
  299.                 return(FALSE);
  300.         }
  301.  
  302.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_PARAMETERS_TXT,Plain))
  303.             return(FALSE);
  304.  
  305.         if(!PrintText(File,ReqWindow,Error,"%ld-%lc-%ld",Entry->Config->SerialConfig->BitsPerChar,Parities[(WORD)Entry->Config->SerialConfig->Parity],Entry->Config->SerialConfig->StopBits))
  306.             return(FALSE);
  307.  
  308.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_HANDSHAKING_TXT,Plain))
  309.             return(FALSE);
  310.  
  311.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_SERIALPANEL_HANDSHAKING_NONE_TXT + Entry->Config->SerialConfig->HandshakingProtocol)))
  312.             return(FALSE);
  313.  
  314.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_DUPLEX_TXT,Plain))
  315.             return(FALSE);
  316.  
  317.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_SERIALPANEL_DUPLEX_FULL_TXT + Entry->Config->SerialConfig->Duplex)))
  318.             return(FALSE);
  319.  
  320.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_STRIP_BIT_TXT,Plain))
  321.             return(FALSE);
  322.  
  323.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_DISABLED_TXT + Entry->Config->SerialConfig->StripBit8)))
  324.             return(FALSE);
  325.  
  326.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_FLOW_CONTROL_TXT,Plain))
  327.             return(FALSE);
  328.  
  329.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_DISABLED_TXT + Entry->Config->SerialConfig->xONxOFF)))
  330.             return(FALSE);
  331.  
  332.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_SERIAL_DRIVER_TXT,Plain))
  333.             return(FALSE);
  334.  
  335.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_NAME_UNIT_TEMPLATE_TXT),Entry->Config->SerialConfig->SerialDevice, + Entry->Config->SerialConfig->UnitNumber))
  336.             return(FALSE);
  337.     }
  338.  
  339.     if((Flags & PRINT_MODEM) && Entry->Config->ModemConfig)
  340.     {
  341.         if(Entry->Config->ModemConfig->ModemInit[0])
  342.         {
  343.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_MODEM_INIT_TXT,Plain))
  344.                 return(FALSE);
  345.  
  346.             if(!PrintText(File,ReqWindow,Error,"\"%s\"",Entry->Config->ModemConfig->ModemInit))
  347.                 return(FALSE);
  348.         }
  349.  
  350.         if(Entry->Config->ModemConfig->ModemExit[0])
  351.         {
  352.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_MODEM_EXIT_TXT,Plain))
  353.                 return(FALSE);
  354.  
  355.             if(!PrintText(File,ReqWindow,Error,"\"%s\"",Entry->Config->ModemConfig->ModemExit))
  356.                 return(FALSE);
  357.         }
  358.  
  359.         if(Entry->Config->ModemConfig->ModemHangup[0])
  360.         {
  361.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_HANG_UP_TXT,Plain))
  362.                 return(FALSE);
  363.  
  364.             if(!PrintText(File,ReqWindow,Error,"\"%s\"",Entry->Config->ModemConfig->ModemHangup))
  365.                 return(FALSE);
  366.         }
  367.  
  368.         if(Entry->Config->ModemConfig->DialPrefix[0])
  369.         {
  370.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_DIAL_PREFIX_TXT,Plain))
  371.                 return(FALSE);
  372.  
  373.             if(!PrintText(File,ReqWindow,Error,"\"%s\"",Entry->Config->ModemConfig->DialPrefix))
  374.                 return(FALSE);
  375.         }
  376.  
  377.         if(Entry->Config->ModemConfig->DialSuffix[0])
  378.         {
  379.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_DIAL_SUFFIX_TXT,Plain))
  380.                 return(FALSE);
  381.  
  382.             if(!PrintText(File,ReqWindow,Error,"\"%s\"",Entry->Config->ModemConfig->DialSuffix))
  383.                 return(FALSE);
  384.         }
  385.  
  386.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_REDIAL_DELAY_TXT,Plain))
  387.             return(FALSE);
  388.  
  389.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_MINUTE_SECOND_TEMPLATE_TXT),Entry->Config->ModemConfig->RedialDelay / 60,Entry->Config->ModemConfig->RedialDelay % 60))
  390.             return(FALSE);
  391.  
  392.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_DIAL_TIMEOUT_TXT,Plain))
  393.             return(FALSE);
  394.  
  395.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_MINUTE_SECOND_TEMPLATE_TXT),Entry->Config->ModemConfig->DialTimeout / 60,Entry->Config->ModemConfig->DialTimeout % 60))
  396.             return(FALSE);
  397.  
  398.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_AUTO_BAUD_TXT,Plain))
  399.             return(FALSE);
  400.  
  401.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_DISABLED_TXT + Entry->Config->ModemConfig->ConnectAutoBaud)))
  402.             return(FALSE);
  403.  
  404.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_DROP_DTR_TXT,Plain))
  405.             return(FALSE);
  406.  
  407.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_DISABLED_TXT + Entry->Config->ModemConfig->DropDTR)))
  408.             return(FALSE);
  409.     }
  410.  
  411.     if((Flags & PRINT_SCREEN) && Entry->Config->ScreenConfig)
  412.     {
  413.         UBYTE ModeNameBuffer[DISPLAYNAMELEN + 1];
  414.  
  415.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_DISPLAY_MODE_TXT,Plain))
  416.             return(FALSE);
  417.  
  418.         GetModeName(Entry->Config->ScreenConfig->DisplayMode,ModeNameBuffer,sizeof(ModeNameBuffer));
  419.  
  420.         if(!PrintText(File,ReqWindow,Error,ModeNameBuffer))
  421.             return(FALSE);
  422.  
  423.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_COLOUR_MODE_TXT,Plain))
  424.             return(FALSE);
  425.  
  426.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_SCREENPANEL_COLOUR_AMIGA_TXT + Entry->Config->ScreenConfig->ColourMode)))
  427.             return(FALSE);
  428.     }
  429.  
  430.     if((Flags & PRINT_TERMINAL) && Entry->Config->TerminalConfig)
  431.     {
  432.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_TERMINAL_EMULATION_TXT,Plain))
  433.             return(FALSE);
  434.  
  435.         if(Entry->Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL)
  436.         {
  437.             if(!PrintText(File,ReqWindow,Error,"%s, \"%s\"",LocaleString(MSG_TERMINALPANEL_EMULATION_ANSI_VT102_TXT + Entry->Config->TerminalConfig->EmulationMode),Entry->Config->TerminalConfig->EmulationMode))
  438.                 return(FALSE);
  439.         }
  440.         else
  441.         {
  442.             if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_TERMINALPANEL_EMULATION_ANSI_VT102_TXT + Entry->Config->TerminalConfig->EmulationMode)))
  443.                 return(FALSE);
  444.         }
  445.  
  446.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_FONT_TXT,Plain))
  447.             return(FALSE);
  448.  
  449.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_TERMINALPANEL_FONT_STANDARD_TXT + Entry->Config->TerminalConfig->FontMode)))
  450.             return(FALSE);
  451.  
  452.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_TEXT_COLUMNS_TXT,Plain))
  453.             return(FALSE);
  454.  
  455.         if(Entry->Config->TerminalConfig->NumColumns < 20)
  456.         {
  457.             if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_MAXIMUM_TXT)))
  458.                 return(FALSE);
  459.         }
  460.         else
  461.         {
  462.             if(!PrintText(File,ReqWindow,Error,"%ld",Entry->Config->TerminalConfig->NumColumns))
  463.                 return(FALSE);
  464.         }
  465.  
  466.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_TEXT_LINES_TXT,Plain))
  467.             return(FALSE);
  468.  
  469.         if(Entry->Config->TerminalConfig->NumLines < 20)
  470.         {
  471.             if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_MAXIMUM_TXT)))
  472.                 return(FALSE);
  473.         }
  474.         else
  475.         {
  476.             if(!PrintText(File,ReqWindow,Error,"%ld",Entry->Config->TerminalConfig->NumLines))
  477.                 return(FALSE);
  478.         }
  479.  
  480.         if(Entry->Config->TerminalConfig->KeyMapFileName[0])
  481.         {
  482.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_KEYMAP_FILE_TXT,Plain))
  483.                 return(FALSE);
  484.  
  485.             if(!PrintText(File,ReqWindow,Error,"\"%s\"",Entry->Config->TerminalConfig->KeyMapFileName))
  486.                 return(FALSE);
  487.         }
  488.     }
  489.  
  490.     return(TRUE);
  491. }
  492.  
  493.     /* PrintScreen(BPTR File,struct Window *ReqWindow,LONG *Error):
  494.      *
  495.      *    Print the contents of the screen, requires the raster
  496.      *    to be available.
  497.      */
  498.  
  499. BOOL
  500. PrintScreen(BPTR File,struct Window *ReqWindow,LONG *Error)
  501. {
  502.     UBYTE *Buffer;
  503.     LONG i,j;
  504.  
  505.         /* Run down the lines... */
  506.  
  507.     for(i = 0 ; i <= LastLine ; i++)
  508.     {
  509.             /* Grab the line. */
  510.  
  511.         Buffer = &Raster[i * RasterWidth];
  512.  
  513.         j = LastColumn;
  514.  
  515.             /* Strip trailing spaces. */
  516.  
  517.         while(j >= 0 && Buffer[j] == ' ')
  518.             j--;
  519.  
  520.             /* Blank line? */
  521.  
  522.         if(j >= 0)
  523.         {
  524.             SetIoErr(0);
  525.  
  526.             if(Write(File,Buffer,j + 1) < j + 1)
  527.             {
  528.                 *Error = IoErr();
  529.  
  530.                 return(FALSE);
  531.             }
  532.         }
  533.  
  534.             /* Is printing to be aborted? */
  535.  
  536.         if(!SysReqHandler(ReqWindow,NULL,FALSE))
  537.         {
  538.             *Error = 0;
  539.  
  540.             return(FALSE);
  541.         }
  542.  
  543.             /* Add line terminator. */
  544.  
  545.         SetIoErr(0);
  546.  
  547.         if(Write(File,"\n",1) < 1)
  548.         {
  549.             *Error = IoErr();
  550.  
  551.             return(FALSE);
  552.         }
  553.  
  554.             /* Is printing to be aborted? */
  555.  
  556.         if(!SysReqHandler(ReqWindow,NULL,FALSE))
  557.         {
  558.             *Error = 0;
  559.  
  560.             return(FALSE);
  561.         }
  562.     }
  563.  
  564.     return(TRUE);
  565. }
  566.  
  567.     /* PrintClip(BPTR File,struct Window *ReqWindow,LONG *Error):
  568.      *
  569.      *    Print the contents of the clipboard.
  570.      */
  571.  
  572. BOOL
  573. PrintClip(BPTR File,struct Window *ReqWindow,LONG *Error)
  574. {
  575.         /* Are we currently reading input from the
  576.          * clipboard? If so, close it.
  577.          */
  578.  
  579.     if(ClipInput)
  580.     {
  581.         CloseClip();
  582.  
  583.         ClipInput = ClipXerox = FALSE;
  584.     }
  585.  
  586.         /* Open the clipboard for reading. */
  587.  
  588.     if(*Error = OpenClip(Config->ClipConfig->ClipboardUnit))
  589.         return(FALSE);
  590.     else
  591.     {
  592.         UBYTE InputBuffer[256];
  593.         LONG Len;
  594.  
  595.             /* Read clipboard contents. */
  596.  
  597.         while((Len = GetClip(InputBuffer,sizeof(InputBuffer) - 1)) > 0)
  598.         {
  599.                 /* Are we to stop printing? */
  600.  
  601.             if(!SysReqHandler(ReqWindow,NULL,FALSE))
  602.             {
  603.                 *Error = 0;
  604.  
  605.                 CloseClip();
  606.  
  607.                 return(FALSE);
  608.             }
  609.             else
  610.             {
  611.                 SetIoErr(0);
  612.  
  613.                 if(Write(File,InputBuffer,Len) < Len)
  614.                 {
  615.                     *Error = IoErr();
  616.  
  617.                     CloseClip();
  618.  
  619.                     return(FALSE);
  620.                 }
  621.             }
  622.         }
  623.  
  624.         if(Len < 0)
  625.         {
  626.             if(SysReqHandler(ReqWindow,NULL,FALSE) == -2)
  627.             {
  628.                 SetIoErr(0);
  629.  
  630.                 if(Write(File,"\n",1) < 1)
  631.                     *Error = IoErr();
  632.  
  633.                 CloseClip();
  634.  
  635.                 return(FALSE);
  636.             }
  637.         }
  638.  
  639.         CloseClip();
  640.     }
  641.  
  642.     return(TRUE);
  643. }
  644.  
  645.     /* PrintBuffer(BPTR File,struct Window *ReqWindow,LONG *Error):
  646.      *
  647.      *    Print the contents of the text buffer.
  648.      */
  649.  
  650. BOOL
  651. PrintBuffer(BPTR File,struct Window *ReqWindow,LONG *Error)
  652. {
  653.     BOOL Continue;
  654.     LONG i,Len;
  655.  
  656.     SafeObtainSemaphoreShared(&BufferSemaphore);
  657.  
  658.     Continue = TRUE;
  659.  
  660.     if(BufferLines)
  661.     {
  662.         for(i = 0 ; i < Lines ; i++)
  663.         {
  664.             Len = BufferLines[i][-1];
  665.  
  666.             if(!SysReqHandler(ReqWindow,NULL,FALSE))
  667.             {
  668.                 *Error = 0;
  669.  
  670.                 Continue = FALSE;
  671.  
  672.                 break;
  673.             }
  674.  
  675.             if(Len)
  676.             {
  677.                 SetIoErr(0);
  678.  
  679.                 if(Write(File,BufferLines[i],Len) < Len)
  680.                 {
  681.                     *Error = IoErr();
  682.  
  683.                     Continue = FALSE;
  684.  
  685.                     break;
  686.                 }
  687.             }
  688.  
  689.             if(!SysReqHandler(ReqWindow,NULL,FALSE))
  690.             {
  691.                 *Error = 0;
  692.  
  693.                 Continue = FALSE;
  694.  
  695.                 break;
  696.             }
  697.  
  698.             SetIoErr(0);
  699.  
  700.             if(Write(File,"\n",1) < 1)
  701.             {
  702.                 *Error = IoErr();
  703.  
  704.                 Continue = FALSE;
  705.  
  706.                 break;
  707.             }
  708.         }
  709.     }
  710.     else
  711.         Continue = FALSE;
  712.  
  713.     ReleaseSemaphore(&BufferSemaphore);
  714.  
  715.     return(Continue);
  716. }
  717.  
  718.     /* PrintSomething(BYTE Source):
  719.      *
  720.      *    Print the screen or the current contents of the clipboard.
  721.      */
  722.  
  723. VOID
  724. PrintSomething(LONG Source)
  725. {
  726.     struct Window *ReqWindow;
  727.     struct EasyStruct Easy;
  728.     LONG Error;
  729.     BOOL CloseIt;
  730.  
  731.         /* Fill in the easy requester structure. */
  732.  
  733.     Easy.es_StructSize        = sizeof(struct EasyStruct);
  734.     Easy.es_Flags            = NULL;
  735.     Easy.es_Title            = (STRPTR)LocaleString(MSG_TERMAUX_TERM_REQUEST_TXT);
  736.     Easy.es_GadgetFormat    = (STRPTR)LocaleString(MSG_PRINT_STOP_TXT);
  737.  
  738.     if(Source == PRINT_CLIP)
  739.         Easy.es_TextFormat = (STRPTR)LocaleString(MSG_PRINT_PRINTING_CLIP_TXT);
  740.     else
  741.         Easy.es_TextFormat = (STRPTR)LocaleString(MSG_PRINT_PRINTING_SCREEN_TXT);
  742.  
  743.     Error = 0;
  744.     CloseIt = FALSE;
  745.  
  746.         /* The requester is to be displayed while printing. */
  747.  
  748.     if(ReqWindow = BuildEasyRequest(Window,&Easy,NULL))
  749.     {
  750.         BPTR SomeFile;
  751.  
  752.             /* Add header information if printer channel is already open. */
  753.  
  754.         if(PrinterCapture)
  755.         {
  756.             LONG Len = strlen(LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_FOLLOWS_TXT));
  757.  
  758.             if(Write(PrinterCapture,LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_FOLLOWS_TXT),Len) < Len)
  759.             {
  760.                 FreeSysRequest(ReqWindow);
  761.  
  762.                 ReqWindow = NULL;
  763.  
  764.                 Error = IoErr();
  765.  
  766.                 SomeFile = NULL;
  767.             }
  768.             else
  769.                 SomeFile = PrinterCapture;
  770.         }
  771.         else
  772.         {
  773.                 /* Open printer channel. */
  774.  
  775.             if(SomeFile = Open("PRT:",MODE_NEWFILE))
  776.                 CloseIt = TRUE;
  777.             else
  778.             {
  779.                 FreeSysRequest(ReqWindow);
  780.  
  781.                 ReqWindow = NULL;
  782.  
  783.                 Error = IoErr();
  784.             }
  785.         }
  786.  
  787.             /* Everything fine so far? */
  788.  
  789.         if(!Error && SomeFile)
  790.         {
  791.             BOOL Stopped;
  792.  
  793.                 /* Are we to print the screen? */
  794.  
  795.             if(Source == PRINT_SCREEN)
  796.                 Stopped = !PrintScreen(SomeFile,ReqWindow,&Error);
  797.             else
  798.                 Stopped = !PrintClip(SomeFile,ReqWindow,&Error);
  799.  
  800.                 /* Add a trailer if necessary. */
  801.  
  802.             if(PrinterCapture)
  803.             {
  804.                 if(!Error && !Stopped)
  805.                 {
  806.                     LONG Len;
  807.  
  808.                     SetIoErr(0);
  809.  
  810.                     Len = strlen(LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_ENDING_TXT));
  811.  
  812.                     if(Write(PrinterCapture,LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_ENDING_TXT),Len) < Len)
  813.                         Error = IoErr();
  814.                 }
  815.             }
  816.         }
  817.  
  818.             /* Close the stream if necessary. */
  819.  
  820.         if(CloseIt)
  821.         {
  822.                 /* Close the printer stream. */
  823.  
  824.             if(!Close(SomeFile))
  825.                 Error = IoErr();
  826.         }
  827.  
  828.             /* Release the system requster. */
  829.  
  830.         if(ReqWindow)
  831.             FreeSysRequest(ReqWindow);
  832.  
  833.             /* Display the error code if necessary. */
  834.  
  835.         if(Error)
  836.         {
  837.             UBYTE LocalBuffer[256];
  838.             STRPTR ErrorString;
  839.  
  840.             if(Fault(Error,NULL,LocalBuffer,sizeof(LocalBuffer)))
  841.                 ErrorString = LocalBuffer;
  842.             else
  843.                 ErrorString = "???";
  844.  
  845.             ShowRequest(Window,LocaleString(MSG_PRINT_ERROR_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),Error,ErrorString);
  846.         }
  847.     }
  848. }
  849.  
  850.     /* PrintRegion(LONG Top,LONG Bottom):
  851.      *
  852.      *    Print the contents of a screen region.
  853.      */
  854.  
  855. VOID
  856. PrintRegion(LONG Top,LONG Bottom,BOOL FormFeed)
  857. {
  858.     BPTR SomeFile;
  859.     LONG i,j;
  860.     UBYTE *Buffer;
  861.  
  862.     BlockWindows();
  863.  
  864.     if(PrinterCapture)
  865.     {
  866.         LONG Len = strlen(LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_FOLLOWS_TXT));
  867.  
  868.         if(Write(PrinterCapture,LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_FOLLOWS_TXT),Len) < Len)
  869.         {
  870.             ShowRequest(Window,LocaleString(MSG_CONSOLE_ERROR_WRITING_TO_PRINTER_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT));
  871.  
  872.             ReleaseWindows();
  873.  
  874.             return;
  875.         }
  876.  
  877.         SomeFile = PrinterCapture;
  878.     }
  879.     else
  880.     {
  881.         if(!(SomeFile = Open("PRT:",MODE_NEWFILE)))
  882.         {
  883.             ShowRequest(Window,LocaleString(MSG_TERMMAIN_FAILED_TO_OPEN_PRINTER_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT));
  884.  
  885.             ReleaseWindows();
  886.  
  887.             return;
  888.         }
  889.     }
  890.  
  891.     for(i = Top ; i < Bottom ; i++)
  892.     {
  893.         Buffer = &Raster[i * RasterWidth];
  894.  
  895.         j = LastColumn;
  896.  
  897.         while(j >= 0 && Buffer[j] == ' ')
  898.             j--;
  899.  
  900.         if(j >= 0)
  901.         {
  902.             SetIoErr(0);
  903.  
  904.             if(Write(SomeFile,Buffer,j + 1) < j + 1)
  905.             {
  906.                 FormFeed = FALSE;
  907.  
  908.                 break;
  909.             }
  910.         }
  911.  
  912.         SetIoErr(0);
  913.  
  914.         if(Write(SomeFile,"\n",1) < 1)
  915.         {
  916.             FormFeed = FALSE;
  917.  
  918.             break;
  919.         }
  920.     }
  921.  
  922.     if(PrinterCapture)
  923.     {
  924.         LONG Len = strlen(LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_ENDING_TXT));
  925.  
  926.         Write(PrinterCapture,LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_ENDING_TXT),Len);
  927.     }
  928.     else
  929.     {
  930.         if(FormFeed)
  931.             Write(SomeFile,"\f",1);
  932.  
  933.         Close(SomeFile);
  934.     }
  935.  
  936.     ReleaseWindows();
  937.  
  938. }
  939.  
  940.     /* PrintScreenGfx():
  941.      *
  942.      *    Print the window contents as graphics.
  943.      */
  944.  
  945. BOOL
  946. PrintScreenGfx()
  947. {
  948.     struct MsgPort *PrintPort;
  949.     LONG Error;
  950.  
  951.         /* Create the printer port */
  952.  
  953.     if(PrintPort = CreateMsgPort())
  954.     {
  955.         struct IODRPReq *PrintRequest;
  956.  
  957.             /* Create the rastport dump request */
  958.  
  959.         if(PrintRequest = (struct IODRPReq *)CreateIORequest(PrintPort,sizeof(struct IODRPReq)))
  960.         {
  961.                 /* Open the printer driver */
  962.  
  963.             if(!OpenDevice("printer.device",0,(struct IORequest *)PrintRequest,NULL))
  964.             {
  965.                 struct RastPort *RPort;
  966.  
  967.                     /* Create a new rastport */
  968.  
  969.                 if(RPort = (struct RastPort *)AllocVecPooled(sizeof(struct RastPort),MEMF_ANY))
  970.                 {
  971.                     struct BitMap *BitMap;
  972.                     LONG Width,Height;
  973.  
  974.                         /* Initialize the rastport */
  975.  
  976.                     InitRastPort(RPort);
  977.  
  978.                         /* Keep these handy */
  979.  
  980.                     Width    = Window->Width        - (Window->BorderLeft + Window->BorderRight);
  981.                     Height    = Window->Height    - (Window->BorderTop + Window->BorderBottom);
  982.  
  983.                     if(StatusWindow)
  984.                         Height -= StatusDisplayHeight;
  985.  
  986.                         /* Allocate offscreen buffer to hold the window contents */
  987.  
  988.                     if(BitMap = CreateBitMap(Width,Height,GetBitMapDepth(Window->RPort->BitMap),NULL,Window->RPort->BitMap))
  989.                     {
  990.                         struct EasyStruct Easy;
  991.                         struct Window *ReqWindow;
  992.  
  993.                             /* Put the bitmap into the rastport */
  994.  
  995.                         RPort->BitMap = BitMap;
  996.  
  997.                             /* Clear the bitmap */
  998.  
  999.                         SetRast(RPort,0);
  1000.  
  1001.                             /* Copy the window contents to the offscreen buffer */
  1002.  
  1003.                         ClipBlit(Window->RPort,Window->BorderLeft,Window->BorderTop,RPort,0,0,Width,Height,MINTERM_COPY);
  1004.  
  1005.                             /* Wait for the bitmap to be transferred */
  1006.  
  1007.                         WaitBlit();
  1008.  
  1009.                             /* Set up the print request */
  1010.  
  1011.                         PrintRequest->io_Command    = PRD_DUMPRPORT;
  1012.                         PrintRequest->io_RastPort    = RPort;
  1013.                         PrintRequest->io_ColorMap    = Window->WScreen->ViewPort.ColorMap;
  1014.                         PrintRequest->io_Modes        = GetVPModeID(&Window->WScreen->ViewPort);
  1015.                         PrintRequest->io_SrcWidth    = Width;
  1016.                         PrintRequest->io_SrcHeight    = Height;
  1017.  
  1018.                             /* Set up the abort requester */
  1019.  
  1020.                         Easy.es_StructSize        = sizeof(Easy);
  1021.                         Easy.es_Flags            = NULL;
  1022.                         Easy.es_Title            = LocaleString(MSG_TERMAUX_TERM_REQUEST_TXT);
  1023.                         Easy.es_GadgetFormat    = LocaleString(MSG_GLOBAL_ABORT_GAD);
  1024.                         Easy.es_TextFormat        = LocaleString(MSG_PRINTING_SCREEN_TXT);
  1025.  
  1026.                             /* Create the abort requester */
  1027.  
  1028.                         if(ReqWindow = BuildEasyRequest(Window,&Easy,NULL))
  1029.                         {
  1030.                             ULONG Signals;
  1031.  
  1032.                                 /* Everything is fine so far */
  1033.  
  1034.                             Error = 0;
  1035.  
  1036.                                 /* Start printing */
  1037.  
  1038.                             BeginIO((struct IORequest *)PrintRequest);
  1039.  
  1040.                                 /* Run until everything is done */
  1041.  
  1042.                             while(TRUE)
  1043.                             {
  1044.                                     /* Wait for an event */
  1045.  
  1046.                                 Signals = Wait(PORTMASK(ReqWindow->UserPort) | PORTMASK(PrintPort));
  1047.  
  1048.                                     /* Is the printer finished? */
  1049.  
  1050.                                 if(Signals & PORTMASK(PrintPort))
  1051.                                 {
  1052.                                         /* Wait for the request to return and check for error */
  1053.  
  1054.                                     switch(WaitIO((struct IORequest *)PrintRequest))
  1055.                                     {
  1056.                                         case PDERR_NOTGRAPHICS:
  1057.  
  1058.                                             Error = ERR_NO_GFX_OUTPUT;
  1059.                                             break;
  1060.  
  1061.                                         case PDERR_BADDIMENSION:
  1062.  
  1063.                                             Error = ERR_BAD_DIMENSION;
  1064.                                             break;
  1065.  
  1066.                                         case IOERR_OPENFAIL:
  1067.  
  1068.                                             Error = ERR_NO_PRINTER;
  1069.                                             break;
  1070.  
  1071.                                         case PDERR_INTERNALMEMORY:
  1072.                                         case PDERR_BUFFERMEMORY:
  1073.  
  1074.                                             Error = ERROR_NO_FREE_STORE;
  1075.                                             break;
  1076.                                     }
  1077.  
  1078.                                     break;
  1079.                                 }
  1080.  
  1081.                                     /* Did the user press the abort button? */
  1082.  
  1083.                                 if(Signals & PORTMASK(ReqWindow->UserPort))
  1084.                                 {
  1085.                                     if(!SysReqHandler(ReqWindow,NULL,FALSE))
  1086.                                     {
  1087.                                         AbortIO((struct IORequest *)PrintRequest);
  1088.  
  1089.                                         WaitIO((struct IORequest *)PrintRequest);
  1090.  
  1091.                                         break;
  1092.                                     }
  1093.                                 }
  1094.                             }
  1095.  
  1096.                                 /* Close the requester */
  1097.  
  1098.                             FreeSysRequest(ReqWindow);
  1099.                         }
  1100.                         else
  1101.                             Error = ERROR_NO_FREE_STORE;
  1102.  
  1103.                             /* Free the memory allocated for the bitmap */
  1104.  
  1105.                         DeleteBitMap(BitMap);
  1106.                     }
  1107.                     else
  1108.                         Error = ERROR_NO_FREE_STORE;
  1109.  
  1110.                         /* Free the rastport */
  1111.  
  1112.                     FreeVecPooled(RPort);
  1113.                 }
  1114.                 else
  1115.                     Error = ERROR_NO_FREE_STORE;
  1116.  
  1117.                     /* Close the printer driver */
  1118.  
  1119.                 CloseDevice((struct IORequest *)PrintRequest);
  1120.             }
  1121.             else
  1122.                 Error = ERR_NO_PRINTER;
  1123.  
  1124.                 /* Free the rastport dump request */
  1125.  
  1126.             DeleteIORequest((struct IORequest *)PrintRequest);
  1127.         }
  1128.         else
  1129.             Error = ERROR_NO_FREE_STORE;
  1130.  
  1131.             /* Free the printer port */
  1132.  
  1133.         DeleteMsgPort(PrintPort);
  1134.     }
  1135.     else
  1136.         Error = ERROR_NO_FREE_STORE;
  1137.  
  1138.         /* Return the result */
  1139.  
  1140.     if(Error)
  1141.     {
  1142.         SetIoErr(Error);
  1143.  
  1144.         return(FALSE);
  1145.     }
  1146.     else
  1147.         return(TRUE);
  1148. }
  1149.